#include <iostream>
#include "MyStack.h"

MyStack::MyStack(int capacity)//类外构造要加域
{
    //初始化
    m_size = 0;
    m_capacity = capacity;
    m_data = new char[m_capacity];
}

MyStack::~MyStack()
{
    delete []m_data;//必须加[],因为是数组
}

int MyStack::reCapacity()//扩容
{
    char * tmpData = m_data;//1.备份

    int newCapacity = m_capacity << 1;//左移1 //2.创建空间
    m_data = new char[newCapacity];

    for(int idx = 0; idx < m_size; idx++)//3.数据迁移
    {
        m_data[idx] = tmpData[idx];
    }

    delete [] tmpData;//4.释放内存

    m_capacity = newCapacity;//5.更新容量

    return 0;
}

void MyStack::push(char data)//入栈
{
    if(m_capacity == m_size)
    {
        reCapacity();
    }
    m_data[m_size++] = data;//插入元素
}

void MyStack::pop()//出栈
{
    m_size--;
}

bool MyStack::empty()//空栈
{
    return this->m_size == 0;
}

char MyStack::top()//栈顶
{
    return m_data[m_size - 1];
}

int MyStack::size()//元素个数
{
    return this->m_size;
}

int MyStack::capacity()
{
    return m_capacity;
}
